<template>
  <div>
    <el-card class="box-card" style="height:1000px">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="科室分类">
          <el-input v-model="formInline.department_type_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="科室名称" style="margin-left: 50px">
          <el-input v-model="formInline.department_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="医生姓名" style="margin-left: 50px">
          <el-input v-model="formInline.doctor_name" disabled></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="病人姓名">
          <el-input v-model="formInline.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="病人性别" style="margin-left: 50px">
          <el-input v-model="formInline.gender_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="出生年月" style="margin-left: 50px">
          <el-input v-model="formInline.data_birth" disabled></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="本人电话">
          <el-input v-model="formInline.mobile" disabled></el-input>
        </el-form-item>
        <el-form-item label="家属电话" style="margin-left: 50px">
          <el-input v-model="formInline.family_mobile"></el-input>
        </el-form-item>
        <el-form-item label="血型" style="margin-left: 50px">
          <el-select v-model="formInline.blood_type" placeholder="请选择">
            <el-option
                v-for="item in blood_options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
          <!--          <el-input v-model=""></el-input>-->
        </el-form-item>
      </el-form>

      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="身份证号">
          <el-input v-model="formInline.id_care"></el-input>
        </el-form-item>

      </el-form>

      <el-form :inline="true" :model="formInline" class="demo-form-inline">

        <el-form-item label="病情自述">
          <el-input
              type="textarea"
              v-model="formInline.illness_desc"
              style="width: 600px"
              disabled
          ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="诊断结果">
          <el-input v-model="formInline.diagnosis"></el-input>
        </el-form-item>
        <el-form-item label="是否建议住院">
          <el-switch v-model="formInline.is_hospital"></el-switch>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="选择药物">
          <el-select v-model="drug.drugType" placeholder="药品类型">
            <el-option v-for="drug in drugType" :key=drug :label=drug :value=drug @click.native="drug_1(drug)">
            </el-option>
          </el-select>
          <el-select
              v-model="drug.drugName"
              multiple
              placeholder="药品名称">
            <el-option v-for="drug in drugName" :key=drug :label=drug :value=drug>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="诊断说明">
          <el-input
              type="textarea"
              v-model="formInline.diagnosis_instructions"
              style="width: 600px"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="drug" class="demo-form-inline">
        <el-row>
          <el-button
              type="danger"
              round
              style="margin-left: 450px"
              @click="goBack"
          >取消
          </el-button
          >
          <el-button
              type="success"
              round
              style="margin-left: 30px"
              @click="diagnosisIsOk"
          >诊断完成
          </el-button
          >
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  mounted() {
    window.Vue = this;
  },
  data() {
    return {
      formInline: {
        department_type_name: "",
        department_name: "",
        doctor_name: "",
        name: "",
        gender_name: "",
        data_birth: "",
        mobile: "",
        family_mobile: "",
        blood_type: "",
        illness_desc: "",
        diagnosis: "",
        is_hospital: false,
        medicine_choice: "",
        diagnosis_instructions: "",
      },
      value: '',
      blood_options: [{
        value: '0',
        label: '暂未查询'
      }, {
        value: '1',
        label: 'O型'
      }, {
        value: '2',
        label: 'A型'
      }, {
        value: '3',
        label: 'B型'
      }, {
        value: '4',
        label: 'AB型'
      }],
      drug: {
        drugType: "",
        drugName: "",
      },
      drugType: [],
      drugName: [],
      init_data: [],
    };
  },
  created() {
    this.selectDrugType();
    this.selectPatientRegisterById();
  },
  methods: {
    selectPatientRegisterById() {
      console.log(this.$route.query.row.id);
      //axios查询病历请求
      this.$axios
          .get(this.$settings.BASE_URL + `registered/registerinfoview/${this.$route.query.row.id}/`)
          .then((Response) => {
            this.formInline = Response.data.info;
          })
          .catch((error) => {
            console.error(error);
          });
    },
    selectDrugType() {
      //axios查询药品类型请求
      this.$axios.get(this.$settings.BASE_URL + 'user/medicine_type/').then((Response) => {
        this.init_data = Response.data.data
        let category_list = []
        for (let dict of Response.data.data) {  // 拿到每一个字典  {'999':[药品名，药品名]},{'伟哥':[药品名，药品名]},{'阿莫西林a':[药品名，药品名]}
          category_list.push(Object.keys(dict)[0])
        }
        this.drugType = category_list;  // ['强身健体', '清热处方', '感冒药', '补肾', '抗生素']
      }).catch((error) => {
        console.error(error);
      });
    },
    selectDrugName(drugType) {
      //根据药品分类查询药品名称
      for (let dict of this.init_data) {  // 拿到每一个字典  {'999':[药品名，药品名]},{'伟哥':[药品名，药品名]},{'阿莫西林a':[药品名，药品名]}
        if (Object.keys(dict)[0] === drugType) {
          for (let name of Object.values(dict)) {
            for (let i of name) {
              this.drugName.push(i)
            }
          }
        }
      }
    },
    drug_1(drug) {
      this.drugName = []
      this.selectDrugName(drug);
    },
    goBack() {
      this.$confirm("确认返回到医生诊断界面？")
          .then((_) => {
            this.$router.push({
              path: "/index/doctorDiagnosis",
            });
            location.reload()
          })
          .catch((_) => {
          });
    },
    diagnosisIsOk() {
      this.$confirm("确认提交诊断？")
          .then((_) => {
            console.log('确定了')
            var drugList = this.drug.drugName;
            var drugResult = "";
            for (var j = 0; j < drugList.length; j++) {
              drugResult = drugResult + "," + drugList[j];
            }
            console.log(drugResult);
            console.log('我在这哦')
            // this.formInline.medicine_choice=''
            this.formInline['medicine_choice'] = drugResult
            console.log(this.formInline.medicine_choice)
            console.log('我已经到这了')
            //axios提交诊断请求
            this.$axios
                .post(this.$settings.BASE_URL + "outpatient/patient_post/",
                    this.formInline
                    // {
                    // id: this.$route.query.row.id,
                    // userAccount: this.$route.query.row.userAccount,
                    // userName: this.$route.query.row.userName,
                    // docAccount: this.$route.query.row.docAccount,
                    // docName: this.$route.query.row.docName,
                    // docType: this.$route.query.row.docType,
                    // docDiagnostic: this.formInline.docDiagnostic,
                    // disease: this.formInline.disease,
                    // isHosAdvice: this.formInline.isHosAdvice,
                    // deptName: this.$route.query.row.deptName,
                    // deptCategoryName: this.$route.query.row.deptCategoryName,
                    // drug: drugResult,
                    // }
                )
                .then((Response) => {
                  if (Response.data.code === 100) {
                    alert("提交诊断成功");
                    this.$axios.delete(this.$settings.BASE_URL + `registered/registerinfoview/${this.$route.query.row.id}/`)
                        .then((Response) => {
                          if (Response.data.code === 100) {
                            this.$router.push({
                              path: "/index/doctorDiagnosis",
                            });
                          }
                        })
                  } else {
                    alert("提交诊断失败");
                  }
                })
                .catch((error) => {
                  console.error(error);
                });
          })
          .catch((_) => {
          });
    },
  },
};
</script>

<style>
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 1080px;
  height: 1000px;
  margin-top: 50px;
}
</style>